package com.cos66.handler;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.cos66.model.HotActivityItem;

public class HotActivityHandler extends DefaultHandler {

	private List<HotActivityItem> data;
	private HotActivityItem item;
	private StringBuilder sb = new StringBuilder();

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		data = new ArrayList<HotActivityItem>();
		super.startDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		sb.setLength(0);
		if (localName.equals("hotactivty")) {
			item = new HotActivityItem();
		}
		super.startElement(uri, localName, qName, attributes);
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		sb.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		String str = sb.toString();
		if (localName.equals("hotActivityImageUrl")) {
			item.setHotActivityImageUrl(str);
		} else if (localName.equals("hotActivityTitle")) {
			item.setHotActivityTitle(str);
		} else if (localName.equals("hotActivityId")) {
			item.setHotActivityId(Long.parseLong(str));
		} else if (localName.equals("hotActivityResUrl")) {
			item.setHotActivityResUrl(str);
		} else if (localName.equals("hotactivty")) {
			data.add(item);
		}
		super.endElement(uri, localName, qName);
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}
	
	public List<HotActivityItem> getData() {
		return data;
	}

}
